package com.xyy.saas.payment.adapter;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.xyy.saas.payment.dao.model.AlipaySettings;
import com.xyy.saas.payment.dao.model.WxSettings;
import com.xyy.saas.payment.service.ChannelSettingsService;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangtianqi
 * @date 2020/11/26 11:04
 */
@Component
public class ChannelSettingManager {

    private static final String ALIPAY_SERVER_URL = "https://openapi.alipay.com/gateway.do";

    @Autowired
    private ChannelSettingsService channelSettingsService;

    public WxSettings getWXSettings(String businessOrderType, String payMode) {
        return channelSettingsService.queryWXByBusinessOrderTypeAndPayMode(businessOrderType, payMode);
    }

    public AlipaySettings getAlipaySettings(String businessOrderType, String payMode) {
        return channelSettingsService.queryAlipayByBusinessOrderTypeAndPayMode(businessOrderType, payMode);
    }

    public AlipaySettings getAlipaySettingsByAliAppId(String appId) {
        AlipaySettings alipaySettings = new AlipaySettings();
        alipaySettings.setAlipayAppId(appId);
        List<AlipaySettings> list = channelSettingsService.queryAlipayAll(alipaySettings);
        if(list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    public WxSettings getWXSettingsByAppIdAndMchId(String appId, String mchId) {
        WxSettings wxSettings = new WxSettings();
        wxSettings.setWxAppId(appId);
        wxSettings.setMerchantId(mchId);
        List<WxSettings> list = channelSettingsService.queryWXAll(wxSettings);
        if(list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    public AlipayClient getAlipayClient(String businessOrderType, String payMode) {
        AlipaySettings alipaySettings = getAlipaySettings(businessOrderType, payMode);
        AlipayClient alipayClient = new DefaultAlipayClient(ALIPAY_SERVER_URL, alipaySettings.getAlipayAppId(), alipaySettings.getAppPrivateKey(), "json", "UTF-8", alipaySettings.getAlipayPublicKey(), alipaySettings.getSignType());
        return alipayClient;
    }

    public Map<String, String> getWXParam(String businessOrderType, String payMode){
        WxSettings settings = getWXSettings(businessOrderType, payMode);
        Map<String, String> settingMap = new HashMap<>();
        settingMap.put("mch_id",settings.getMerchantId());
        settingMap.put("appid",settings.getWxAppId());
        settingMap.put("apiKey",settings.getApiSecret());
        return settingMap;
    }

    public WxSettings getWXSettingByMchId(String mchId) {
        return getWXSettingsByMchId(mchId).get(NumberUtils.INTEGER_ZERO);
    }

    public List<WxSettings> getWXSettingsByMchId(String mchId) {
        WxSettings wxSettings = new WxSettings();
        wxSettings.setMerchantId(mchId);
        return channelSettingsService.queryWXAll(wxSettings);
    }
}
